import request from "../../utils/request";
const app = getApp();
// pages/workbench/workbench.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    awaitPrice: null,
    totalOut: null,
    totalWait: null,
    totalPrice: null,
    payOrder: 0,
    awaitPayOrder:0,
    payOrderList: [],
    awaitPayOrderList: [],
    loginShow: false,
    payOutPhone: null,
    payOutPrice: null,
    popupShow: false,
    payOutFunction: "wechat",
    showsex: false,
    payBank: "",
    payBankCardNumber: null,
    payBankName: "",
    columns: ["中国银行", "中国农业银行", "中国工商银行", "中国建设银行"],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getWalletMessage();
    this.getWalletAllMoney();
    this.getOrderMsg();
    this.getAwaitOrderMsg();
  },
  gocollection() {
    wx.navigateTo({
      url: "/pages/collection/collection",
    });
  },
  goback() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  goperformance() {
    wx.navigateTo({
      url: "/pages/performance/performance",
    });
  },
  goposter() {
    wx.navigateTo({
      url: "/pages/poster/poster",
    });
  },
  goDeposit() {
    wx.navigateTo({
      url: "/pages/deposit/deposit",
    });
  },
  gowithdrawList() {
    wx.navigateTo({
      url: "/pages/withdrawList/withdrawList",
    });
  },
  godaili() {
    wx.navigateTo({
      url: `/pages/daili/daili?titlle=${"修改信息"}`,
    });
  },
  // 获取总收入和以体现金额
  async getWalletMessage() {
    const proxyId = wx.getStorageSync("proxyId");
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/in-money-record/getInMoneyOfAll?proxyId=${proxyId}`,
      method: "GET",
    });
    this.setData({
      awaitPrice: (res.data.totalIn / 100).toFixed(2),
      totalOut: Math.abs(res.data.totalOut / 100).toFixed(2),
      totalWait: Math.abs(res.data.totalWait / 100).toFixed(2),
    });
  },
  // 获取待提现金额
  async getWalletAllMoney() {
    const proxyId = wx.getStorageSync("proxyId");
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/in-money/getIn?pageNo=${1}&pageSize=${50}&proxyId=${proxyId}`,
      method: "GET",
    });
    this.setData({
      totalPrice: (res.data.totalPrice / 100).toFixed(2),
    });
  },
  // 获取订单详情
  async getOrderMsg() {
    const proxyId = wx.getStorageSync("proxyId");
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/trade-order/getOrderProxy?pageNo=${1}&pageSize=${50}&proxyUserId=${proxyId}&payStatus=${0}`,
    });
    if(res.data.total !== null){
      this.setData({
        payOrder: res.data.total,
      });
    }
  },
  //  获取待付款订单详情
  async getAwaitOrderMsg() {
    const proxyId = wx.getStorageSync("proxyId");
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/trade-order/getOrderProxy?pageNo=${1}&pageSize=${50}&proxyUserId=${proxyId}&payStatus=${1}`,
    });
    if (res.data.total !== null) {
      this.setData({
        awaitPayOrder: res.data.total,
      });
    }
  },
  goMerchantMessage() {
    wx.navigateTo({
      url: `/pages/MerchantMessage/MerchantMessage?dataList=${this.data.payOrderList}`,
    });
  },
  goAwaitPayOrder() {
    wx.navigateTo({
      url: `/pages/awaitPayOrder/awaitPayOrder?dataList=${this.data.awaitPayOrderList}`,
    });
  },
  takeOut() {
    this.setData({
      loginShow: true,
    });
  },
  onCloseLogin() {
    this.setData({
      loginShow: false,
      value: "",
      payOutPrice: "",
      payBankName: "",
      payBank: "",
      payBankCardNumber: "",
      payOutPhone: "",
    });
  },
  async getPayOut() {
    console.log("1", this.data.payOutPrice);
    if (this.data.payOutPrice < 100) {
      wx.showToast({
        title: "单次提现金额不得低于100",
        icon: "none",
      });
    } else {
      if (this.data.payOutFunction === "wechat") {
        if (
          this.data.payOutPrice === null ||
          this.data.payOutPhone.length !== 11 ||
          this.data.payOutPrice === ""
        ) {
          wx.showToast({
            title: "提现金额或电话号码位数不正确请核验后再申请",
            icon: "none",
            duration: 3000,
          });
        } else {
          const proxyId = wx.getStorageSync("proxyId");
          const datalist = {
            proxyId: proxyId,
            transferType: 1,
            price: parseFloat((this.data.payOutPrice * 100).toPrecision(2)),
            accountNo: this.data.payOutPhone,
          };
          this.payOut(datalist);
        }
      } else if (this.data.payOutFunction === "zhifubao") {
        if (
          this.data.payOutPrice === null ||
          this.data.payOutPhone.length !== 11 ||
          this.data.payOutPrice === ""
        ) {
          wx.showToast({
            title: "提现金额或电话号码位数不正确请核验后再申请",
            icon: "none",
            duration: 3000,
          });
        } else {
          const proxyId = wx.getStorageSync("proxyId");
          const datalist = {
            proxyId: proxyId,
            transferType: 1,
            price: parseFloat((this.data.payOutPrice * 100).toPrecision(2)),
            accountNo: this.data.payOutPhone,
          };
          this.payOut(datalist);
        }
      } else if (this.data.payOutFunction === "bankCard") {
        if (
          this.data.payOutPrice === null ||
          this.data.payBankName === null ||
          this.data.payOutPrice === "" ||
          this.data.payBankName === ""
        ) {
          wx.showToast({
            title: "提现金额或者收款人姓名为空，请完善信息后再重新申请",
            icon: "none",
            duration: 3000,
          });
        } else if (
          this.data.payBank === null ||
          this.data.payBankCardNumber === null ||
          this.data.payBankCardNumber === "" ||
          this.data.payBankCardNumber.length < 16
        ) {
          wx.showToast({
            title:
              "您的提现银行还未选择或收款银行卡号填写有误，请核对后再重新申请",
            icon: "none",
            duration: 3000,
          });
        } else {
          const proxyId = wx.getStorageSync("proxyId");
          const datalist = {
            proxyId: proxyId,
            transferType: 3,
            price: parseFloat(((this.data.payOutPrice) * 100).toPrecision(3)),
            accountName: this.data.payBankName,
            accountType: this.data.payBank,
            accountNo: this.data.payBankCardNumber,
          };
          this.payOut(datalist);
        }
      }
    }
  },
  payMethod() {
    this.setData({
      popupShow: true,
    });
  },
  async payOut(datalist) {
    const res = await request({
      url: app.globalData.baseURL + "/app-api/package/out-money/create",
      method: "POST",
      data: datalist,
    });
    if (res.code === 0) {
      wx.showToast({
        title: "申请提现成功，可前往提现记录查看提现进度",
        icon: "none",
        duration: 3000,
      });
      this.getWalletAllMoney()
      this.setData({
        payOutPrice: "",
        payBankName: "",
        payBank: "",
        payBankCardNumber: "",
        payOutPhone: "",
        loginShow: false,
      });
    } else {
      wx.showToast({
        title: res.msg,
        icon: "none",
      });
    }
  },
  onClosePopup() {
    this.setData({
      popupShow: false,
    });
  },
  gobank() {
    this.setData({
      showsex: true,
    });
  },
  onCancelSex() {
    this.setData({
      showsex: false,
    });
  },
  onConfirmSex(event) {
    this.setData({
      payBank: event.detail.value,
      showsex: false,
    });
  },
  useWechat() {
    this.setData({
      payOutFunction: "wechat",
      popupShow: false,
      payOutPrice: "",
      payOutPhone: "",
    });
  },
  useZhifubao() {
    this.setData({
      payOutFunction: "zhifubao",
      popupShow: false,
      payOutPrice: "",
      payOutPhone: "",
    });
  },
  useBankCard() {
    this.setData({
      payOutFunction: "bankCard",
      popupShow: false,
      payOutPrice: "",
      payBankName: "",
      payBank: "",
      payBankCardNumber: "",
    });
  },
  outPrice(event) {
    this.setData({
      payOutPrice:event.detail.value,
    });
  },
  outPhoneNumber(event) {
    this.setData({
      payOutPhone: event.detail.value,
    });
  },
  outBankName(event) {
    this.setData({
      payBankName: event.detail.value,
    });
  },
  outBankCardNumber(event) {
    this.setData({
      payBankCardNumber: event.detail.value,
    });
  },
  /*
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getWalletAllMoney()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
